<?

define("_1ST"                  , 0);
define("_2ND"                  , 1);
define("_DEFAULT_INCREMENT"    , 1);

/**
* check if serial number is exist
* YES: process which warehouse this model in
* NO : Check if warehouse is not null 
*      YES: Process lucky warehouse
*      NO : process this warehouse
*       
*/
$CFetchers->fetchLstDataDetail($COracle,
                               $thisJobId,
                               $srcDbDtl,
                               $nDetail,
                               $lstDetail);

for($iRow = 0; $iRow < $nDetail; $iRow++){
  
  if($lstDetail["SER_NUMBER"][$iRow] != ""){
    
    /**
    * Extract the serial number
    *
    */
    $theSerial = explode(_DEFAULT_SEPARATOR_02,
                         $lstDetail["SER_NUMBER"][$iRow]);
    
    
    /**
    * if manual key in 
    * YES: - Fetch how many entry of this serialNumber
    *        since the qty is equal to how many serNumber been entried
    *      - process the model and set to _PBL_RETUR
    *      - Fetch the source of warehouse of this serNumber
    *      - decrease the inventory balance
    * NO : - Fetch the source of warehouse of this serNumber
    *      - decrease the inventory balance
    *
    */
    if($theSerial[_1ST] == _MANUAL_KEY_IN){
    
      fetchManInDetail($COracle,
                       $theSerial[_2ND],
                       $srcDbManInDtl,
                       $retManInRows,
                       $retManInDetail);
                       
      for($iDtl = 0; $iDtl < $retManInRows; $iDtl++){
        
        fetchStatusModel($COracle,
                         $retManInDetail["SER_NUMBER"][$iDtl],
                         $srcDbModelHistory,
                         $isModelExists,
                         $statusTx);
        
        if($statusTx != _PBL_RETUR){
          
          $isModelClean = true;
        }else{
          
          $isModelClean = false;
        }
        
        if($isModelExists == true &&
           $isModelClean  == true){
          
          $modelId = NULL;
          $unit    = NULL;
          
          fetchModelHistoryDtl($COracle,
                               $retManInDetail["SER_NUMBER"][$iDtl],
                               $srcDbModelHistory,
                               $nRowsMdlHstDetail,
                               $modelHistoryDetail);
                               
          $txType   = _PBL_RETUR;
          
          $srcDate    = date("d-m-Y",
                             strtotime($modelHistoryDetail["TR_DATE"][0]));
                             
          $trDate     = " TO_DATE('".$srcDate."','dd-mm-YYYY') ";
  
          $lastNumber = $modelHistoryDetail["NO"][0] + _DEFAULT_INCREMENT;
          
          $values = "  ".$lastNumber." ,                         ".  
                    "  ".$trDate." ,                             ".
                    " '".$modelHistoryDetail["SRC_REF"][0]."',   ".
                    "  ".$modelHistoryDetail["SRC_NO"][0]." ,    ".
                    " '".$modelHistoryDetail["MODEL"][0]."',     ".
                    "  ".$modelHistoryDetail["MODEL_ID"][0]." ,  ".
                    " '".$modelHistoryDetail["UNIT"][0]."',      ".
                    " '".$modelHistoryDetail["SER_NUMBER"][0]."' ";
                    
          insToHistory($COracle,
                       $values,
                       $txType,
                       $srcDbModelHistory);
          
          fetchSrcWarehouse($COracle,
                            $modelHistoryDetail["SRC_REF"][0],
                            $srcDbSrcHdr,
                            $srcDbGudang);
          
          updateInvBalance($COracle,
                           $lstDetail["MODEL"][$iRow],
                           $modelId,
                           $unit,
                           -1,          /* Default decrement, -1 */
                           $srcDbGudang);
        }else{
          
          if($isModelExists == false){
            
            usrDieJs("Error: Model with this serial number ".$retManInDetail["SER_NUMBER"][$iDtl]." 
                             is not exists ");
          }else{
            
            if($isModelClean == false){
              
              usrDieJs("Error: Model with this serial number is not clean ");
            }else{
              
              /* Do nothing */
            }
          }
        }
      }
      
    }else{
      
      fetchStatusModel($COracle,
                       $lstDetail["SER_NUMBER"][$iRow],
                       $srcDbModelHistory,
                       $isModelExists,
                       $isModelClean);
      
      if($statusTx != _PBL_RETUR){
          
        $isModelClean = true;
      }else{
        
        $isModelClean = false;
      }
      
      if($isModelExists == true &&
         $isModelClean  == true){
        
        $modelId = NULL;
        $unit    = NULL;
          
        fetchModelHistoryDtl($COracle,
                             $retManInDetail["SER_NUMBER"][$iDtl],
                             $srcDbModelHistory,
                             $nRowsMdlHstDetail,
                             $modelHistoryDetail);
                             
        $txType   = _PBL_RETUR;
        
        $srcDate    = date("d-m-Y",
                           strtotime($modelHistoryDetail["TR_DATE"][0]));
                           
        $trDate     = " TO_DATE('".$srcDate."','dd-mm-YYYY') ";

        $lastNumber = $modelHistoryDetail["NO"][0] + _DEFAULT_INCREMENT;
        
        $values = "  ".$lastNumber." ,                         ".  
                  "  ".$trDate." ,                             ".
                  " '".$modelHistoryDetail["SRC_REF"][0]."',   ".
                  "  ".$modelHistoryDetail["SRC_NO"][0]." ,    ".
                  " '".$modelHistoryDetail["MODEL"][0]."',     ".
                  "  ".$modelHistoryDetail["MODEL_ID"][0]." ,  ".
                  " '".$modelHistoryDetail["UNIT"][0]."',      ".
                  " '".$modelHistoryDetail["SER_NUMBER"][0]."' ";
                  
        insToHistory($COracle,
                     $values,
                     $txType,
                     $srcDbModelHistory);
        
        fetchSrcWarehouse($COracle,
                          $modelHistoryDetail["SRC_REF"][0],
                          $srcDbSrcHdr,
                          $srcDbGudang);
        
        updateInvBalance($COracle,
                         $lstDetail["MODEL"][$iRow],
                         $modelId,
                         $unit,
                         -1,          /* Default decrement, -1 */
                         $srcDbGudang);
      }else{
        
        if($isModelExists == false){
            
          usrDieJs("Error: Model with this serial number ".$retManInDetail["SER_NUMBER"][$iDtl]." 
                           is not exists ");
        }else{
          
          if($isModelClean == false){
            
            usrDieJs("Error: Model with this serial number is not clean ");
          }else{
            
            /* Do nothing */
          }
        }
      }
    }
    
  }else{
    
    /**
    * Make sure that the balance 
    * doesn't run negative
    *
    */
    if($lstDetail["WAREHOUSE"][$iRow] == ""){
      
      fetchActiveWarehouse($COracle,
                           $srcDbMstWarehouse,
                           $nWarehouse,
                           $srcDbGudang);
      
      for($iDtl = 0; $iDtl < $lstDetail["QTY"][$iRow]; $iDtl++){
        
        for($iWhs = 0; $iWhs < $nWarehouse; $iWhs++){
          
          $modelId = NULL;
          $unit    = NULL;
          
          $theWarehouse = "WHS_". $srcDbGudang[$iWhs];
          
          fetchLastBalance($COracle,
                           $lstDetail["MODEL"][$iRow],
                           $modelId,
                           $unit,
                           $theWarehouse,
                           $modelBalance,
                           $isProcess);
                           
          if($isProcess){
            
            updateInvBalance($COracle,
                             $lstDetail["MODEL"][$iRow],
                             $modelId,
                             $unit,
                             -1,          /* Default decrement, -1 */
                             $theWarehouse);
          
            break;
          }else{
            
            $isLast = $iDtl + 1;
            
            if($nWarehouse == $isLast){
              
              usrDieJs("Error: Insufficient Balance of this model ".$lstDetail["MODEL"][$iRow]." ");
            }else{
              
              /* Do nothing */
            }
          }                 
        }
      }
    }else{
      
      $theWarehouse = "WHS_". $lstDetail["WAREHOUSE"][$iRow];
      
      fetchLastBalance($COracle,
                       $lstDetail["MODEL"][$iRow],
                       $modelId,
                       $unit,
                       $theWarehouse,
                       $modelBalance,
                       $isProcess);
      
      $chk = $lastBalance - $lstDetail["QTY"][$iRow];
      
      if($chk >= 0){
        
        updateInvBalance($COracle,
                         $lstDetail["MODEL"][$iRow],
                         $modelId,
                         $unit,
                         -1,          /* Default decrement, -1 */
                         $theWarehouse);
      }else{
        
        usrDie("Error: Insufficient Balance of this model ".$lstDetail["MODEL"][$iRow]." ");
      }
    }
  }
}

?>